#!/usr/bin/env python
import vtk
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# this script tests vtkImageResliceToColors with various axes permutations,
# in order to cover a nasty set of "if" statements that check
# the intersections of the raster lines with the input bounding box.
# Image pipeline
reader = vtk.vtkImageReader()
reader.ReleaseDataFlagOff()
reader.SetDataByteOrderToLittleEndian()
reader.SetDataExtent(0,63,0,63,1,93)
reader.SetDataSpacing(3.2,3.2,1.5)
reader.SetFilePrefix("" + str(VTK_DATA_ROOT) + "/Data/headsq/quarter")
reader.SetDataMask(0x7fff)
# a regular lookup table
table = vtk.vtkLookupTable()
table.SetRange(0,2000)
table.SetRampToLinear()
table.SetAlphaRange(1.0,1.0)
table.SetValueRange(0.0,1.0)
table.SetSaturationRange(1.0,0.0)
table.SetHueRange(0.0,0.1)
table.Build()
# map RGB floats
table2 = vtk.vtkLookupTable()
table2.SetRange(0.0,1.0)
table2.SetVectorModeToRGBColors()
# map RGB unsigned chars
table3 = vtk.vtkLookupTable()
table3.SetRange(0,255)
table3.SetVectorModeToRGBColors()
colors = vtk.vtkImageMapToColors()
colors.SetInputConnection(reader.GetOutputPort())
colors.SetLookupTable(table)
colors.SetOutputFormatToRGB()
floats = vtk.vtkImageShiftScale()
floats.SetInputConnection(colors.GetOutputPort())
floats.SetShift(0.0)
floats.SetScale(0.0039215686274509803)
floats.SetOutputScalarTypeToFloat()
transform = vtk.vtkTransform()
# rotate about the center of the image
transform.Translate(+100.8,+100.8,+69.0)
transform.RotateWXYZ(10,1,1,0)
transform.Translate(-100.8,-100.8,-69.0)
reslice1 = vtk.vtkImageResliceToColors()
reslice1.SetInputConnection(colors.GetOutputPort())
reslice1.SetResliceAxesDirectionCosines([1,0,0,0,1,0,0,0,1])
reslice1.SetLookupTable(table3)
reslice1.SetResliceTransform(transform)
reslice1.SetOutputSpacing(3.2,3.2,3.2)
reslice1.SetOutputExtent(0,74,0,74,0,0)
reslice1.SetBackgroundColor(0,0,127,255)
reslice1.Update()
reslice2 = vtk.vtkImageResliceToColors()
reslice2.SetInputConnection(colors.GetOutputPort())
reslice2.SetResliceAxesDirectionCosines([0,1,0,0,0,1,1,0,0])
reslice2.SetOutputSpacing(3.2,3.2,3.2)
reslice2.SetOutputExtent(0,74,0,74,0,0)
reslice2.SetOutputFormatToLuminance()
reslice2.SetBackgroundColor(0,0,127,255)
reslice3 = vtk.vtkImageResliceToColors()
reslice3.SetInputConnection(floats.GetOutputPort())
reslice3.SetLookupTable(table2)
reslice3.SetResliceAxesDirectionCosines([0,0,1,1,0,0,0,1,0])
reslice3.SetResliceTransform(transform)
reslice3.SetOutputSpacing(3.2,3.2,3.2)
reslice3.SetOutputExtent(0,74,0,74,0,0)
reslice3.SetOutputFormatToRGB()
reslice3.SetBackgroundColor(0,0,127,255)
reslice4 = vtk.vtkImageResliceToColors()
reslice4.SetInputConnection(floats.GetOutputPort())
reslice4.SetLookupTable(table2)
reslice4.SetResliceAxesDirectionCosines([-1,0,0,0,-1,0,0,0,-1])
reslice4.SetOutputSpacing(3.2,3.2,3.2)
reslice4.SetOutputExtent(0,74,0,74,0,0)
reslice4.SetBackgroundColor(0,0,127,255)
reslice5 = vtk.vtkImageResliceToColors()
reslice5.SetInputConnection(reader.GetOutputPort())
reslice5.SetLookupTable(table)
reslice5.SetResliceAxesDirectionCosines([0,-1,0,0,0,-1,-1,0,0])
reslice5.SetResliceTransform(transform)
reslice5.SetOutputSpacing(3.2,3.2,3.2)
reslice5.SetOutputExtent(0,74,0,74,0,0)
reslice5.SetBackgroundColor(0,0,127,255)
reslice6 = vtk.vtkImageResliceToColors()
reslice6.SetInputConnection(reader.GetOutputPort())
reslice6.SetLookupTable(table)
reslice6.SetResliceAxesDirectionCosines([0,0,-1,-1,0,0,0,-1,0])
reslice6.SetOutputSpacing(3.2,3.2,3.2)
reslice6.SetOutputExtent(0,74,0,74,0,0)
reslice6.SetOutputFormatToLuminance()
reslice6.SetBackgroundColor(0,0,127,255)
mapper1 = vtk.vtkImageMapper()
mapper1.SetInputConnection(reslice1.GetOutputPort())
mapper1.SetColorWindow(255.0)
mapper1.SetColorLevel(127.5)
mapper1.SetZSlice(0)
mapper2 = vtk.vtkImageMapper()
mapper2.SetInputConnection(reslice2.GetOutputPort())
mapper2.SetColorWindow(255.0)
mapper2.SetColorLevel(127.5)
mapper2.SetZSlice(0)
mapper3 = vtk.vtkImageMapper()
mapper3.SetInputConnection(reslice3.GetOutputPort())
mapper3.SetColorWindow(255.0)
mapper3.SetColorLevel(127.5)
mapper3.SetZSlice(0)
mapper4 = vtk.vtkImageMapper()
mapper4.SetInputConnection(reslice4.GetOutputPort())
mapper4.SetColorWindow(255.0)
mapper4.SetColorLevel(127.5)
mapper4.SetZSlice(0)
mapper5 = vtk.vtkImageMapper()
mapper5.SetInputConnection(reslice5.GetOutputPort())
mapper5.SetColorWindow(255.0)
mapper5.SetColorLevel(127.5)
mapper5.SetZSlice(0)
mapper6 = vtk.vtkImageMapper()
mapper6.SetInputConnection(reslice6.GetOutputPort())
mapper6.SetColorWindow(255.0)
mapper6.SetColorLevel(127.5)
mapper6.SetZSlice(0)
actor1 = vtk.vtkActor2D()
actor1.SetMapper(mapper1)
actor2 = vtk.vtkActor2D()
actor2.SetMapper(mapper2)
actor3 = vtk.vtkActor2D()
actor3.SetMapper(mapper3)
actor4 = vtk.vtkActor2D()
actor4.SetMapper(mapper4)
actor5 = vtk.vtkActor2D()
actor5.SetMapper(mapper5)
actor6 = vtk.vtkActor2D()
actor6.SetMapper(mapper6)
imager1 = vtk.vtkRenderer()
imager1.AddActor2D(actor1)
imager1.SetViewport(0.0,0.0,0.3333,0.5)
imager2 = vtk.vtkRenderer()
imager2.AddActor2D(actor2)
imager2.SetViewport(0.0,0.5,0.3333,1.0)
imager3 = vtk.vtkRenderer()
imager3.AddActor2D(actor3)
imager3.SetViewport(0.3333,0.0,0.6667,0.5)
imager4 = vtk.vtkRenderer()
imager4.AddActor2D(actor4)
imager4.SetViewport(0.3333,0.5,0.6667,1.0)
imager5 = vtk.vtkRenderer()
imager5.AddActor2D(actor5)
imager5.SetViewport(0.6667,0.0,1.0,0.5)
imager6 = vtk.vtkRenderer()
imager6.AddActor2D(actor6)
imager6.SetViewport(0.6667,0.5,1.0,1.0)
imgWin = vtk.vtkRenderWindow()
imgWin.AddRenderer(imager1)
imgWin.AddRenderer(imager2)
imgWin.AddRenderer(imager3)
imgWin.AddRenderer(imager4)
imgWin.AddRenderer(imager5)
imgWin.AddRenderer(imager6)
imgWin.SetSize(230,150)
imgWin.Render()
# --- end of script --
